package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.QwhBankJournal;
import com.ruoyi.system.service.IQwhBankJournalService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 银行日记账Controller
 * 
 * @author ruoyi
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/journal")
public class QwhBankJournalController extends BaseController
{
    @Autowired
    private IQwhBankJournalService qwhBankJournalService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询银行日记账列表
     */
    @RequiresPermissions("system:journal:list")
    @GetMapping("/list")
    public TableDataInfo list(QwhBankJournal qwhBankJournal)
    {
        startPage();
        List<QwhBankJournal> list = qwhBankJournalService.selectQwhBankJournalList(qwhBankJournal);
        redisTemplate.opsForValue().set("bank",qwhBankJournal.getBankId());
        return getDataTable(list);
    }

    /**
     * 导出银行日记账列表
     */
    @RequiresPermissions("system:journal:export")
    @Log(title = "银行日记账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QwhBankJournal qwhBankJournal)
    {
        List<QwhBankJournal> list = qwhBankJournalService.selectQwhBankJournalList(qwhBankJournal);
        ExcelUtil<QwhBankJournal> util = new ExcelUtil<QwhBankJournal>(QwhBankJournal.class);
        util.exportExcel(response, list, "银行日记账数据");
    }

    /**
     * 获取银行日记账详细信息
     */
    @RequiresPermissions("system:journal:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(qwhBankJournalService.selectQwhBankJournalById(id));
    }

    /**
     * 新增银行日记账
     */
    @RequiresPermissions("system:journal:add")
    @Log(title = "银行日记账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwhBankJournal qwhBankJournal)
    {
        Object bank = redisTemplate.opsForValue().get("bank");
        qwhBankJournal.setBankId(Long.parseLong(bank.toString()));
        return toAjax(qwhBankJournalService.insertQwhBankJournal(qwhBankJournal));
    }

    /**
     * 修改银行日记账
     */
    @RequiresPermissions("system:journal:edit")
    @Log(title = "银行日记账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwhBankJournal qwhBankJournal)
    {
        return toAjax(qwhBankJournalService.updateQwhBankJournal(qwhBankJournal));
    }

    @Log(title = "银行日记账", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:journal:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<QwhBankJournal> util = new ExcelUtil<QwhBankJournal>(QwhBankJournal.class);
        List<QwhBankJournal> qwhBankJournals = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = qwhBankJournalService.importUser(qwhBankJournals, updateSupport, operName);
        return success(message);
    }







    /**
     * 删除银行日记账
     */
    @RequiresPermissions("system:journal:remove")
    @Log(title = "银行日记账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwhBankJournalService.deleteQwhBankJournalByIds(ids));
    }

}
